package com.onlineMIS.action.headQ.supplier.purchase;


import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.onlineMIS.ORM.DAO.Response;
import com.onlineMIS.ORM.DAO.chainS.ChainUtility;
import com.onlineMIS.ORM.DAO.headQ.barCodeGentor.ProductBarcodeService;
import com.onlineMIS.ORM.entity.chainS.user.ChainUserInfor;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.ProductBarcode;
import com.onlineMIS.ORM.entity.headQ.finance.FinanceBill;
import com.onlineMIS.ORM.entity.headQ.inventory.InventoryOrder;
import com.onlineMIS.ORM.entity.headQ.inventory.InventoryOrderVO;
import com.onlineMIS.ORM.entity.headQ.inventory.JinSuanOrderTemplate;
import com.onlineMIS.ORM.entity.headQ.supplier.purchase.PurchaseOrder;
import com.onlineMIS.ORM.entity.headQ.supplier.purchase.PurchaseOrderVO;
import com.onlineMIS.ORM.entity.headQ.supplier.purchase.SupplierPurchaseOrderTemplate;
import com.onlineMIS.ORM.entity.headQ.user.UserInfor;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.loggerLocal;
import com.opensymphony.xwork2.ActionContext;

@Controller
public class SupplierPurchaseJSPAction extends SupplierPurchaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1120066931973798941L;
	/**
	 * the inventory order template
	 */
	private String excelFileName = "CaiGouDan.xls";
	
	//采购单据模板
	private final String supplierOrderTemplate = "SupplierSalesTemplate.xls";
	
	@Autowired
	protected ProductBarcodeService productService;
	
	private InputStream excelStream;
	
	public InputStream getExcelStream() {
		return excelStream;
	}

	public void setExcelStream(InputStream excelStream) {
		this.excelStream = excelStream;
	}

	public String getExcelFileName() {
		return excelFileName;
	}

	public void setExcelFileName(String excelFileName) {
		this.excelFileName = excelFileName;
	}
	/**
	 * 获取单据
	 * @return
	 * @throws Exception 
	 */
	public String getPurchaseOrder() throws Exception{
	    UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		int orderId = formBean.getOrder().getId();
		loggerLocal.info(loginUserInfor.getUser_name() + " : SupplierPurchaseJSPAction.getPurchaseOrder , " + orderId);
		
		Response response = supplierPurchaseService.loadPurchaseOrder(orderId, loginUserInfor);
		
		if (!response.isSuccess()){
			addActionError(response.getMessage());
			return ERROR;
		} else {
		    int action = response.getAction();
		    switch (action) {
				case 1: //修改单据
					PurchaseOrder order = (PurchaseOrder)response.getReturnValue();
					formBean.setOrder(order);	
					ChainUtility.calculateParam(formBean, order);
					supplierPurchaseService.prepareEditPurchasePage(uiBean);	
					return "editPurchase";
				case 2: //打开已经过账单据
					PurchaseOrder order2 = (PurchaseOrder)response.getReturnValue();
					formBean.setOrder(order2);
					ChainUtility.calculateParam(formBean, order2);
					supplierPurchaseService.prepareEditPurchasePage(uiBean);
					return "viewPurchase";
				default:
					return ERROR;
			}
		}
	}
	
	/**
	 * 进入purchase order搜索页面
	 * @return
	 */
	public String preSearchPurchase(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : SupplierPurchaseJSPAction.preSearchPurchase");
		
		supplierPurchaseService.prepareSearchPurchasePage(formBean);
		
		return "searchPurchase";
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception 
	 */
	public String preEditPurchase() throws Exception{
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : SupplierPurchaseJSPAction.preEditPurchase");
		
		supplierPurchaseService.prepareEditPurchasePage(uiBean);
		
		formBean.getOrder().setType(PurchaseOrder.TYPE_PURCHASE);
		PurchaseOrder pOrder = new PurchaseOrder();
		formBean.getOrder().setTypeHQS(pOrder.getTypeHQMap().get(PurchaseOrder.TYPE_PURCHASE));
		
		return "editPurchase";
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception 
	 */
	public String preEditPurchaseReturn() throws Exception{
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : SupplierPurchaseJSPAction.preEditPurchaseReturn");
		
		supplierPurchaseService.prepareEditPurchasePage(uiBean);
		
		formBean.getOrder().setType(PurchaseOrder.TYPE_RETURN);
		PurchaseOrder pOrder = new PurchaseOrder();
		formBean.getOrder().setTypeHQS(pOrder.getTypeHQMap().get(PurchaseOrder.TYPE_RETURN));
		return "editPurchase";
	}

	/**
	 * 
	 * @return
	 * @throws Exception 
	 */
	public String preEditPurchaseFree() throws Exception{
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : SupplierPurchaseJSPAction.preEditPurchaseFree");
		
		supplierPurchaseService.prepareEditPurchasePage(uiBean);
		
		formBean.getOrder().setType(PurchaseOrder.TYPE_FREE);
		PurchaseOrder pOrder = new PurchaseOrder();
		formBean.getOrder().setTypeHQS(pOrder.getTypeHQMap().get(PurchaseOrder.TYPE_FREE));
		return "editPurchase";
	}
	
	/**
	 * 准备采购订单导入
	 * @return
	 */
	public String preUploadFile(){
		return "preUploadFile";
	}
	
	/**
	 * 把采购单据导出到excel
	 * @return
	 */
	public String downloadPurchase(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : SupplierPurchaseJSPAction.downloadPurchase");
		
		HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);   
		String contextPath= request.getRealPath("/");

		Map<String,Object> map= supplierPurchaseService.generateExcelReport(formBean.getOrder(),contextPath + "WEB-INF\\template\\headQ\\" + SupplierPurchaseOrderTemplate.FILE_NAME);   
		excelStream=(InputStream)map.get("stream");  
		
		return "excelObject";
	}
	
	/**
	 * 导出条码打印(补充字段信息)
	 * @return
	 */
	public String exportOrder(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : SupplierPurchaseJSPAction.downloadPurchase");
		
		HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);   
		String contextPath= request.getRealPath("/");
		
		Map<String,Object> map= productService.generatePurchaseExcelReport(formBean.getOrder().getId(),contextPath + "WEB-INF\\template\\" + JinSuanOrderTemplate.JinsuanFileName);   
		excelStream=(InputStream)map.get("stream");  
		
		return "excelObject";
	}
	
	
	/**
	 * 下载销售报表
	 * @return
	 */																					
	public String downloadOrderInfos() {
		loggerLocal.info(this.getClass().getName() + ".downloadOrderInfos");
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext()
				.get(ServletActionContext.HTTP_REQUEST);
		String contextPath = request.getRealPath("/");
		Response response = new Response();
		try {
			String sort = "startTime";
			String order = "desc";
			Map<String, Object> data = (Map<String, Object>) supplierPurchaseService.searchPurchaseOrder(formBean).getReturnValue();
			List<PurchaseOrderVO> list  = (List<PurchaseOrderVO>) data.get("rows");
			response = supplierPurchaseService.generateSupplierOrderExcel(contextPath + "WEB-INF\\template\\" + supplierOrderTemplate,list);
		} catch (Exception e) {
			response.setReturnCode(Response.FAIL);
			response.setMessage(e.getMessage());
		}

		if (response.getReturnCode() == Response.SUCCESS) {
			InputStream excelStream = (InputStream) response.getReturnValue();
			formBean.setFileStream(excelStream); 
			formBean.setFileName(supplierOrderTemplate);
			return "download";						
		} else
			return ERROR;
	}
}
